import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_test/lf_test_view.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/utils/lf_logger.dart';

class LfHorizontalScrollItem extends StatefulWidget {
  const LfHorizontalScrollItem(
      {super.key,
      required this.max,
      required this.scrollOffset,
      required this.list,
      this.onTap,
      required this.selectedValue});
  final List<TestDataType> list;
  final Function(int value)? onTap;
  final int selectedValue;
  final RxDouble max;
  final RxDouble scrollOffset;
  @override
  State<LfHorizontalScrollItem> createState() => _LfHorizontalScrollItemState();
}

class _LfHorizontalScrollItemState extends State<LfHorizontalScrollItem> {
  List<Widget> child = [];
  double width = ScreenUtil().screenWidth;
  @override
  void initState() {
    super.initState();
    for (var i = 0; i < widget.list.length; i++) {
      child.add(InkWell(
        onTap: () {
          widget.onTap?.call(widget.list[i].value);
          lfLogger.printInfo(
              info: containsNum(widget.selectedValue, widget.list[i].value)
                  .toString());
        },
        child: Container(
          height: 40.r,
          padding: EdgeInsets.symmetric(horizontal: 15.w),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: 20.r.radius,
            border: Border.all(
                color: containsNum(widget.selectedValue, widget.list[i].value)
                    ? Colors.amber
                    : Colors.grey),
            color: containsNum(widget.selectedValue, widget.list[i].value)
                ? Colors.amber
                : Colors.transparent,
          ),
          margin: EdgeInsets.symmetric(horizontal: 5.r),
          child: Text(
            widget.list[i].text,
            style: lfTextStyle(sp: 14.sp),
          ),
        ),
      ));
    }

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (mounted) {
        width = context.size?.width ?? 360;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      var x = ((widget.max.value + ScreenUtil().screenWidth) - width) /
          (widget.max.value) *
          widget.scrollOffset.value;
      return Container(
        transform: Matrix4.translationValues(x, 0.0, 0.0),
        margin: EdgeInsets.symmetric(vertical: 5.r, horizontal: 15.w),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: child,
        ),
      );
    });
  }
}
